﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLCHBANGIAY.DATA;
using QLCHBANGIAY.BUSINESS;
using QLCHBANGIAY.COMMON;

namespace QuanLyBanGiay.ucControl
{
    public partial class ucSidebar : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Viewsidebar();
                ViewCategory();
                ViewNhanhieu();
                ViewGia();
                if (Session["cat"] != null)
                {
                    drlNhom.SelectedValue = Session["cat"].ToString();
                }
                if (Session["nh"] != null)
                {
                    drlNhanhieu.SelectedValue = Session["nh"].ToString();
                }
                if (Session["gia"] != null)
                {
                    drlGia.SelectedValue = Session["gia"].ToString();
                }
                if (Session["k"] != null)
                {
                    txtkeyword.Text = Session["k"].ToString();
                }
            }

        }
        void ViewCategory()
        {
            drlNhom.Items.Clear();
            drlNhom.Items.Add(new ListItem("-Loại sản phẩm-", "0"));
            List<Category> list = CategoryServices.Category_GetByAll("vi");
            for (int i = 0; i < list.Count; i++)
            {
                ListItem lst = new ListItem(StringClass.ShowNameLevel_Cach(list[i].Name, list[i].Level), list[i].Id);
                drlNhom.Items.Add(lst);
            }
            list.Clear();
            list = null;
        }

        void ViewNhanhieu()
        {
            drlNhanhieu.Items.Clear();
            drlNhanhieu.Items.Add(new ListItem("-Nhãn hiệu-", "0"));
            //List<Data.Brands> list = Business.BrandsService.Brands_GetByAll("vi");
            List<Category> list = CategoryServices.Category_GetByAll("vi");
            //for (int i = 0; i < list.Count; i++)
            //{
            //    ListItem lst = new ListItem(StringClass.ShowNameLevel_Cach(list[i].Name, list[i].Level), list[i].Id);
            //    drlNhom.Items.Add(lst);
            //}
            //List<Category> list = CategoryServices.Category_GetByAll("");
            for (int i = 0; i < list.Count; i++)
            {
                drlNhanhieu.Items.Add(new ListItem(list[i].Name, list[i].Id));
            }
            list.Clear();
            list = null;
        }

        void ViewGia()
        {
            drlGia.Items.Clear();
            drlGia.Items.Add(new ListItem("-Chọn giá (VNĐ)-", "0"));
            drlGia.Items.Add(new ListItem("Dưới 200.000", "200000"));
            drlGia.Items.Add(new ListItem("200.000 - 500.000", "500000"));
            drlGia.Items.Add(new ListItem("500.000 - 800.000", "800000"));
            drlGia.Items.Add(new ListItem("800.000 - 1.000.000", "1000000"));
            drlGia.Items.Add(new ListItem("Trên 1.000.000", "1001000"));
        }

        protected void imgbtn_Click(object sender, ImageClickEventArgs e)
        {
            if (drlNhom.SelectedValue != "0" || drlNhanhieu.SelectedValue != "0" || drlGia.SelectedValue != "0" || txtkeyword.Text != "")
            {
                string Chuoi = " 1=1 ";
                if (drlNhom.SelectedValue != "0") { Chuoi += " and CatId=" + drlNhom.SelectedValue; }
              //  if (drlNhanhieu.SelectedValue != "0") { Chuoi += " and BrandId=" + drlNhanhieu.SelectedValue; }
                if (drlGia.SelectedValue != "0")
                {
                    if (drlGia.SelectedValue == "200000")
                    {
                        Chuoi += " and Price<200000 ";
                    }
                    else if (drlGia.SelectedValue == "500000")
                    {
                        Chuoi += " and Price>199000 and Price<501000";
                    }
                    else if (drlGia.SelectedValue == "800000")
                    {
                        Chuoi += " and Price>499000 and Price<801000";
                    }
                    else if (drlGia.SelectedValue == "1000000")
                    {
                        Chuoi += " and Price>799000 and Price<1001000";
                    }
                    else
                    {
                        Chuoi += " and Price>1000000 ";
                    }
                }
                if (txtkeyword.Text != "")
                {
                    Chuoi += " and Name like N'%" + txtkeyword.Text + "%'";
                }

                Session["key"] = Chuoi;
                Session["cat"] = drlNhom.SelectedValue;
                Session["nh"] = drlNhanhieu.SelectedValue;
                Session["gia"] = drlGia.SelectedValue;
                Session["k"] = txtkeyword.Text;
                Response.Redirect("timkiemsanpham.aspx");
            }
        }
        public void Viewsidebar()
        {
            //<div class="sidebar_box"><span class="bottom"></span>
            //    <h3>Categories</h3>   
            //    <div class="content"> 
            //        <ul class="sidebar_list">
            //            <li class="first"><a href="#">Sed eget purus</a></li>
            //            <li><a href="#">Nullam ultrices</a></li>
            //            <li class="last"><a href="#">Sed eget purus</a></li>
            //        </ul>
            //    </div>
            //</div>--%>
            string Chuoi = "";


            List<Category> list = new List<Category>();
            list = CategoryServices.Category_GetByTop("100", "len([Level])=10 and Active=1 and [Level] like '00001%'", "[Level] asc");
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {

                    Chuoi += "<li><a href=\"Phanloaisanpham.aspx?tag=" + list[i].Tag  + "\">" + list[i].Name + "</a></li>";
                }
            }


            ltrSidebarbex.Text = Chuoi;
            ltrSidebarbex = null;
        }
    }
}